<?php

namespace app\admin\controller;

use think\facade\Request;
use app\common\Common;
use app\admin\service\SearchService;

class Search extends Base
{
    private $service;

    public function __construct(SearchService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染列表页面
    public function index()
    {
        return view();
    }

    // 渲染详情页面
    public function info()
    {
        return view();
    }

    // 设置热门搜索列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getHotSearchList($params);

        return Common::success($data);
    }

    // 热门搜索详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Search::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getHotSearchDetail($params['id']);
        return Common::success($data);
    }

    // 添加设置显示在搜索页的热门搜索
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Search::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editHotSearchData($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 编辑设置显示在搜索页的热门搜索
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Search::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editHotSearchData($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 删除设置显示在搜索页的热门搜索
    public function delete()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Search::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deleteHotSearchData($params['id']);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
}